const suffixMap = {
    // image
    png: 'image',
    jpg: 'image',
    jpeg: 'image',
    svg: 'image',

    // text
    txt: 'text',
    html: 'text',
    md: 'text',
    xml: 'text',
    yaml: 'text',
    yml: 'text',
    json: 'text',

    // pdf
    pdf: 'pdf',

    // office
    doc: 'office',
    docx: 'office',
    xls: 'office',
    xlsx: 'office',
    ppt: 'office',
    pptx: 'office',

    // audio
    mp3: 'audio',
    ogg: 'audio',
    wma: 'audio',
    aac: 'audio',
    wav: 'audio',

    // cad
    dwg: 'cad',
    dxf: 'cad',

    // video
    mp4: 'video',
    webm: 'video',
    avi: 'video'
};

export const previewTypes = ['text', 'image', 'pdf', 'office', 'video', 'audio', 'cad'];

export const editTypes = ['office'];

export const printTypes = ['text', 'image', 'pdf', 'office'];

function resolveSuffix(name = '') {
    return name.slice(name.lastIndexOf('.') + 1).toLowerCase();
}

export function getType(name) {
    if (!name) return '';
    const type = suffixMap[resolveSuffix(name)];
    return type || '';
}

export function canPreview(name) {
    return previewTypes.includes(getType(name));
}

export function canEdit(name) {
    return editTypes.includes(getType(name));
}

export function canPrint(name) {
    return printTypes.includes(getType(name));
}

export function getSuffixesByType(type) {
    return Object.entries(suffixMap)
        .filter(([k, v]) => v === type)
        .map(([k, v]) => k);
}
